package com.scedu.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.scedu.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @RequestMapping(value = "/j1")
   // @ResponseBody //用他就不会走仕途解析器，会直接返回一个字符串
    public String json1() throws JsonProcessingException {
        User user1=new User("张三1", 11,"男");
        User user2=new User("张三2", 12,"女");
        User user3=new User("张三3", 13,"男");
        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        //使用jackson，
        ObjectMapper mapper=new ObjectMapper();
        String s = mapper.writeValueAsString(userList);
        return s;
    }
    @RequestMapping(value = "/j2")
    public String json2() throws JsonProcessingException {
        Date date =new Date();
        //自定义日期格式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //使用jackson，
        ObjectMapper mapper=new ObjectMapper();
        //不使用时间戳
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        //设置自定义日期格式
        mapper.setDateFormat(sdf);
        //时间解析后的默认格式为Timestamp，时间戳
        String s = mapper.writeValueAsString(date);
        return s;
    }
    @RequestMapping(value = "/j3")
    // @ResponseBody //用他就不会走仕途解析器，会直接返回一个字符串
    public String json3()  {
        User user1=new User("张三1", 11,"男");
        User user2=new User("张三2", 12,"女");
        User user3=new User("张三3", 13,"男");
        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        //使用FastJson，
        String s = JSON.toJSONString(userList);
        return s;
    }
}
